package es.uned.ii.si3.web.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import es.uned.ii.si3.business.GestionSolicitudes;
import es.uned.ii.si3.dao.SolicitudDAOImpl;
import es.uned.ii.si3.model.Solicitud;
import es.uned.ii.si3.util.UserAgentDetector;
import es.uned.ii.si3.web.actions.generic.LoginRequiredAction;


public class VerSolicitudAction extends LoginRequiredAction {
	private static Logger log = Logger.getLogger(VerSolicitudAction.class);

    public ActionForward executeWithAccess(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {
    	
    	UserAgentDetector uaDetector = new UserAgentDetector(request);

		GestionSolicitudes gestionSolicitudes = new GestionSolicitudes();
		
		int idUsuario = this.getIdUsuario();
		
		

		if (request.getParameter("idSolicitud") != null) {
			
			int idSolicitud = -1;
			try {
				idSolicitud = Integer.parseInt(request.getParameter("idSolicitud"));
			} catch (Exception e) {
				return mapping.findForward(uaDetector.composeForward("nohaySolicitud"));
			}
			log.debug("VerSolicitudAction:La solicitud que se va a consultar es:"+idSolicitud);
			Solicitud solicitud= gestionSolicitudes.getSolicitudById(idSolicitud);
			String urlDocumento = "upload/" + solicitud.getUrlDocumento();
			request.setAttribute("urlDocumento",urlDocumento);		
			request.setAttribute("solicitud", solicitud);
			return mapping.findForward(uaDetector.composeForward("verSolicitud"));
			
		} else {
			return mapping.findForward(uaDetector.composeForward("nohaySolicitud"));
			
		}
    }
    
    
}
